<?php
/**
 * 留言板模型
 * Enter description here ...
 * @author Code.404
 *
 */
import('@.ORG.FAC');
import('@.ORG.SF_PAGE');

class GuestbookModel extends Model{
	const NOT_AUDIT 		= 0;//未通过审核
	const HAS_AUDIT	 		= 1;//已通过审核
	const TRASH				= 2;//垃圾箱
	
	/**
	 * 留言
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	function leaveMessage($data) {
		$_data['guest_user'] = FAC::F($data['guest_user'] , FAC::USERNAME);
		$_data['guest_email'] = FAC::F($data['guest_email'] , FAC::EMAIL);
		$_data['guest_tel'] = FAC::F($data['guest_tel'] , FAC::TEL);
		$_data['title'] = FAC::F($data['title'] , FAC::STRING);
		$_data['message'] = FAC::F($data['message'] , FAC::STRING);
		$_data['leave_time'] = date('Y-m-d H:i:s' , time());
		$_data['is_audit'] = C('GUESTBOOK_NEED_AUDIT') == 1 ? 0 : 1;
		return $this->add($_data);
	}
	/**
	 * 获取指定类型的留言
	 * Enter description here ...
	 * @param unknown_type $type
	 * @param unknown_type $page
	 * @param unknown_type $order
	 */
	function getMessageList($type , &$page , $order = 'is_read asc , leave_time desc , id desc' ){
		$_map['is_audit'] = $type;
		return SF_PAGE::page($this, $page , $order , $_map  );
	}
	/**
	 * 获取指定留言
	 * Enter description here ...
	 * @param unknown_type $id
	 */
	function getMessageById($id){
		$this->find(FAC::F($id , FAC::INT));
		if($this->is_read == 0) {
			$this->is_read = 1;
			$this->save();
		}
		return $this->find(FAC::F($id , FAC::INT));
	}
	/**
	 * 删除留言
	 * @see Model::delete()
	 */
	function delete($id) {
		$id = FAC::F($id , FAC::INT);
		$this->find($id);
		if($this->is_audit == 2){
			return parent::delete($id);
		}else{
			$this->is_audit = 2;
			return $this->save();
		}
	}
	/**
	 * 修改审核状态
	 * Enter description here ...
	 * @param unknown_type $id
	 */
	function changeAudit($id){
		$id = FAC::F($id , FAC::INT);
		$this->find($id);
		$this->is_audit = $this->is_audit == 1 ? 0 : 1;
		return $this->save();
	}
}